/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.ui.controls;

import static ac.settings.dataModel.tableAddstree;
import ac.ui.custemItmes;
import ac.uitls.propertiesOperation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author ismail
 */
public class PCLGetdata extends JComboBox<Object> {

    private String addtype;

    private boolean required;

    private String type;

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private boolean category;

    //   public final Vector model = new Vector();
    propertiesOperation op = new propertiesOperation();

    public final String addvalue = "<-" + op.getWord("controls.add") + "->";

    public boolean getCategory() {
        return category;
    }

    public void setCategory(boolean category) {
        this.category = category;
    }

    public void redef() {

        String[] v = db.getRow(tableAddstree, "`id`,`title`", "def='1' and type='" + getAddtype() + "'");
        appendValue(v[0], v[1]);

    }

    public void appendValue(String v1, String v2) {
        Vector mymodel = new Vector();
        //Model  mymodel=  this.getModel();

        mymodel.addElement(new Item("", ""));
        mymodel.addElement(new Item(addvalue, addvalue));
        mymodel.addElement(new Item(v1, v2));
        setModel(new DefaultComboBoxModel(mymodel));

        // ac.ui.utils.comBoxSetSelectonByValue(this, v[0]);
    }

    public String getAddtype() {
        return addtype;
    }

    public void setAddtype(String addtype) {
        this.addtype = addtype;
    }

    public PCLGetdata() {

        appendValue("", "");

        addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) getItemAt(getSelectedIndex());

                if (itemCate.getValue() == addvalue) {

                    addData();
                }

            }
        });

    }
    ac.lib.database.operations db = new ac.lib.database.operations();

    public void updateValue(String id) {
        ac.uitls.xml x = new ac.uitls.xml();
        Map m;
        m = x.getTreeAdds(getAddtype());

        if (getCategory()) {

            String[] v = db.getRow(tableAddstree, "`id`,`title`", "id='" + id + "'");

            appendValue(v[0], v[1]);

            ac.ui.utils.comBoxSetSelectonByValue(this, v[0]);
        } else {
            String tabel = "" + m.get("table");
            String name = "" + m.get("fieldname");
            String[] v = db.getRow(tabel, "`id`,`" + name + "`", "id='" + id + "'");

            appendValue(v[0], v[1]);

            ac.ui.utils.comBoxSetSelectonByValue(this, v[0]);

        }

    }

    public String convertValue(String id) {

        String returnData = "";
        if (id != null) {

            ac.uitls.xml x = new ac.uitls.xml();
            Map m;
            m = x.getTreeAdds(getAddtype());

            if (getCategory()) {

                String[] v = db.getRow(tableAddstree, "`id`,`title`", "id='" + id + "'");

                returnData = v[1];

            } else {
                String tabel = "" + m.get("table");
                String name = "" + m.get("fieldname");
                String[] v = db.getRow(tabel, "`id`,`" + name + "`", "id='" + id + "'");

                returnData = v[1];
            }
        }

        return returnData;
    }

    private void addData() {
        ac.uitls.xml x = new ac.uitls.xml();
        Map m;
        m = x.getTreeAdds(getAddtype());

        String type = "" + m.get("type");

        String title = "" + m.get("title");

        if (getCategory()) {

            ac.GUI.controls.getCategoryf p = new ac.GUI.controls.getCategoryf(type);
            p.Type = type;
            p.parent = this;
            p.setTitle(op.getWord(title));

            p.setVisible(true);
        } else {

            String tabel = "" + m.get("table");
            ac.GUI.controls.getData1 p = new ac.GUI.controls.getData1(type, tabel);
            p.Type = type;
            p.parent = this;

            p.setTitle(op.getWord(title));

            p.setVisible(true);
        }

    }

    public String getValue() {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getSelectedItem();
            returnData = itemCate.getValue();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public String getText() {

        String returnData = "";
        try {
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getSelectedItem();
            returnData = itemCate.getText();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public void setSelectonByValue(String value) {

        for (int i = 0; i < this.getItemCount(); i++) {
            if (value != null) {
                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) this.getItemAt(i);
///if (!value.equals("")){
                if (itemCate.getValue().equals(value)) {
                    this.setSelectedIndex(i);
                }
            }
            ///  }

        }

    }

    public void setFill(String table, String valueFiled, String TextFilde) {
        Vector model = new Vector();
        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(table);
        try {
            while (rs.next()) {
                model.addElement(new Item(rs.getString(valueFiled), rs.getString(TextFilde)));
            }
        } catch (SQLException ex) {
            Logger.getLogger(custemItmes.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setModel(new DefaultComboBoxModel(model));
        db.setCloseCon();
    }

    public void setFill(String[] data) {
        Vector model = new Vector();
        for (String d : data) {
            model.addElement(new Item(d, d));
        }
        this.setModel(new DefaultComboBoxModel(model));

    }

    public void setFill(String[] value, String[] text) {
        Vector model = new Vector();
        for (int i = 0; i < text.length; i++) {
            model.addElement(new Item(value[i], text[i]));
        }
        this.setModel(new DefaultComboBoxModel(model));
    }

    public void setAppend(String[] value, String[] text) {
        for (int i = 0; i < text.length; i++) {
            //   com.model.addElement(new Item(value[i], text[i]));

            this.appendValue(value[i], text[i]);

        }
        this.repaint();
    }

    public void setAppends(String[] value, String[] text) {
        for (int i = 0; i < text.length; i++) {

            this.appendValue(value[i], text[i]);

        }
        //    com.repaint();
    }

}
